package com.qhs.sys.controller;

import com.qhs.sys.entity.SmsLog;
import com.qhs.sys.exception.BusinessException;
import com.qhs.sys.service.SmsService;
import com.qhs.framework.utils.Result;
import com.qhs.framework.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *  短信消息服务
 * Created by xinchao on 2016/12/7 0007.
 */
@RestController
@RequestMapping(value = "/sms")
public class SmsRestController {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private SmsService smsService;

    /**
     * 该方法用来获取短信验证码
     *
     * @param phone
     * @param codeType
     * @return
     */
    @RequestMapping(value = "/mes", method = RequestMethod.POST)
    public Result login(@RequestParam String phone, @RequestParam String codeType) {
        if(StringUtil.isEmptyIgnoreBlank(phone)){
            return new Result("手机号码不能为空");
        }
        if(StringUtil.isEmptyIgnoreBlank(phone)){
            return new Result("验证码类型不能为空");
        }
        SmsLog smsLog = new SmsLog();
        smsLog.setPhone(phone);
        smsLog.setType(StringUtil.StringToInt(codeType));
        try {
            smsService.insertSms(smsLog);
        } catch (BusinessException e) {
            return new Result(e.getMsg());
        }
        return new Result(true, "消息已成功发送",smsLog);
    }


    /**
     *  校验手机验证码
     *
     * @param phone
     * @param verifyCode
     * @param codeType
     * @return
     */
    @RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
    public Result verifyCode(@RequestParam String phone, @RequestParam String verifyCode, @RequestParam String codeType) {
        if(StringUtil.isEmptyIgnoreBlank(phone)){
            return new Result("手机号码不能为空");
        }
        if(StringUtil.isEmptyIgnoreBlank(verifyCode)){
            return new Result("验证码不能为空");
        }
        if(StringUtil.isEmptyIgnoreBlank(phone)){
            return new Result("验证码类型不能为空");
        }

        try {
            smsService.verifySmsCode(phone,verifyCode,codeType);
        } catch (BusinessException e) {
            return new Result(e.getMsg());
        }
        return new Result(true, "验证码正确");
    }



}
